/*
 * @Author: xiaolong.luo@flowportal xiaolong.luo8888
 * @Date: 2024-05-13 14:20:09
 * @LastEditors: xiaolong.luo@flowportal xiaolong.luo8888
 * @LastEditTime: 2024-05-16 08:59:32
 * @FilePath: \v2-project\src\permission.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import router from "@/router"
import store from "@/store"

import nProgress from "nprogress"
import "nprogress/nprogress.css"
//白名单
const whiteList = ['/login', '/register', '/404']
//导航守卫=>全局前置守卫
router.beforeEach((to, from, next) => {
    // nProgress.start()
    //to 到哪里去
    //from 从哪里来
    //next 函数，决定是否放行,next()表示放行,next('路径')表示跳转到指定路径，next(false)表示不放行
    const token = store.state.auth.token

    if (token) {
        if (to.path === '/login') {
            next('/')
        } else {
            next()
        }
    } else {
        //白名单可直接访问
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next('/login')
        }
    }

})

router.afterEach(() => {
    // nProgress.done();
})

export default router